package com.apps.ftapp.fragment;


import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.apps.ftapp.R;
import com.apps.ftapp.bean.ExamQuestion;
import com.apps.ftapp.bean.QuestionEntry;
import com.apps.ftapp.ui.IExamResponseListener;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.ScreenUtils;
import com.zzhoujay.richtext.ImageHolder;
import com.zzhoujay.richtext.RichText;
import com.zzhoujay.richtext.callback.ImageFixCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 其它题.
 */
public class ExamMaterialFragment extends Fragment {


    private static final String ARG_QUESTION = "arg_question";

    @BindView(R.id.txt_exam_title)
    TextView mTxtTitle;
    @BindView(R.id.txt_exam_progress)
    TextView mTxtProgress;
    @BindView(R.id.txt_exam_content)
    TextView mTxtContent;
    Unbinder unbinder;
    @BindView(R.id.txt_exam_progress_left)
    TextView txtExamProgressLeft;
    @BindView(R.id.xinjia)
    TextView xinjia;
    @BindView(R.id.rl_exam_title)
    RelativeLayout rlExamTitle;
    @BindView(R.id.daana)
    TextView daana;
    @BindView(R.id.jiexiyixia)
    LinearLayout jiexiyixia;
    @BindView(R.id.buzhichi)
    TextView buzhichi;


    private IExamResponseListener mResponseListener;
    int position_whichOne;//第几个题
    List<String> selectedIds;
    Map<String, Boolean> selectedMap;

    ExamQuestion mExamQuestion;
    QuestionEntry mQuestionEntry;

    int screen_width;

    public ExamMaterialFragment() {
        // Required empty public constructor
    }

    public static ExamMaterialFragment newInstance(QuestionEntry question) {

        Bundle args = new Bundle();
        args.putParcelable(ARG_QUESTION, question);

        ExamMaterialFragment fragment = new ExamMaterialFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof IExamResponseListener) {
            mResponseListener = (IExamResponseListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mQuestionEntry = getArguments().getParcelable(ARG_QUESTION);

            mExamQuestion = mQuestionEntry.getExamQuestion();
            position_whichOne = mQuestionEntry.getPosition();
        }


        screen_width = ScreenUtils.getScreenWidth(getContext()) - getResources().getDimensionPixelSize(R.dimen.fab_margin) * 2;

        selectedIds = new ArrayList<>();
        selectedMap = new HashMap<>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_exam_material, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        refreshView();
    }

    public void refreshView() {
        L.i("main", "========");
        mTxtTitle.setText(mQuestionEntry.getTitle());
        txtExamProgressLeft.setText((position_whichOne + 1) + "");
        mTxtProgress.setText("/" + mQuestionEntry.getCount());
        xinjia.setText(mQuestionEntry.getLmname());
        buzhichi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //

                jiexiyixia.setVisibility(View.VISIBLE);

                //
            }
        });


//        RichText.from("【多选题】" + mExamQuestion.getStem())
        RichText.from(mExamQuestion.getStem())
                .autoFix(false)
                .scaleType(ImageHolder.ScaleType.FIT_XY)
                .resetSize(true)
                .size(ImageHolder.WRAP_CONTENT, ImageHolder.WRAP_CONTENT)
                .fix(new ImageFixCallback() {
                    @Override
                    public void onInit(ImageHolder holder) {

                    }

                    @Override
                    public void onLoading(ImageHolder holder) {

                    }

                    @Override
                    public void onSizeReady(ImageHolder holder, int imageWidth, int imageHeight, ImageHolder.SizeHolder sizeHolder) {

                    }

                    @Override
                    public void onImageReady(ImageHolder holder, int width, int height) {

                        if (width * 2 > screen_width) {
                            holder.setWidth(screen_width);
                            holder.setHeight(height * 2 * screen_width / (width * 2));
                        } else {
                            holder.setWidth(width * 2);
                            holder.setHeight(height * 2);
                        }

                    }

                    @Override
                    public void onFailure(ImageHolder holder, Exception e) {

                    }
                }).into(mTxtContent);


        //答案
        RichText.from(mExamQuestion.getAnalysis())
                .autoFix(false)
                .scaleType(ImageHolder.ScaleType.FIT_XY)
                .resetSize(true)
                .size(ImageHolder.WRAP_CONTENT, ImageHolder.WRAP_CONTENT)
                .fix(new ImageFixCallback() {
                    @Override
                    public void onInit(ImageHolder holder) {

                    }

                    @Override
                    public void onLoading(ImageHolder holder) {

                    }

                    @Override
                    public void onSizeReady(ImageHolder holder, int imageWidth, int imageHeight, ImageHolder.SizeHolder sizeHolder) {

                    }

                    @Override
                    public void onImageReady(ImageHolder holder, int width, int height) {

                        if (width * 2 > screen_width) {
                            holder.setWidth(screen_width);
                            holder.setHeight(height * 2 * screen_width / (width * 2));
                        } else {
                            holder.setWidth(width * 2);
                            holder.setHeight(height * 2);
                        }

                    }

                    @Override
                    public void onFailure(ImageHolder holder, Exception e) {

                    }
                }).into(daana);
//
        //错误分析模式
        if (mQuestionEntry.getExam_or_analysis() == 1) {
//            mLlAnalysis.setVisibility(View.VISIBLE);
//            RichText.fromHtml(mExamQuestion.getAnalysis())
//                    .autoFix(false)
//                    .scaleType(ImageHolder.ScaleType.FIT_XY)
//                    .resetSize(true)
//                    .size(ImageHolder.WRAP_CONTENT, ImageHolder.WRAP_CONTENT)
//                    .fix(new ImageFixCallback() {
//                        @Override
//                        public void onInit(ImageHolder holder) {
//
//                        }
//
//                        @Override
//                        public void onLoading(ImageHolder holder) {
//
//                        }
//
//                        @Override
//                        public void onSizeReady(ImageHolder holder, int imageWidth, int imageHeight, ImageHolder.SizeHolder sizeHolder) {
//
//                        }
//
//                        @Override
//                        public void onImageReady(ImageHolder holder, int width, int height) {
//
//                            if (width * 2 > screen_width) {
//                                holder.setWidth(screen_width);
//                                holder.setHeight(height * 2 * screen_width / (width * 2));
//                            } else {
//                                holder.setWidth(width * 2);
//                                holder.setHeight(height * 2);
//                            }
//
//                        }
//
//                        @Override
//                        public void onFailure(ImageHolder holder, Exception e) {
//
//                        }
//                    }).into(mTxtAnalysis);
//            mLlNote.setVisibility(View.VISIBLE);
//            mLlNote.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    Intent intent = new Intent(getActivity(), NoteActivity.class);
//                    intent.putExtra("question", mExamQuestion);
//                    startActivity(intent);
//                }
//            });
//            mTxtNote.setText("编辑笔记");
//
//            if (mExamQuestion.getNote() != null) {
//                ExamNote note = mExamQuestion.getNote();
//
//                if (!TextUtils.isEmpty(note.getContent())) {
//                    String content = "<p>" + note.getContent() + "</p>";
//                    if (!TextUtils.isEmpty(note.getImg_one()))
//                        content += "<img src='" + note.getImg_one() + "'/>";
//
//                    if (!TextUtils.isEmpty(note.getImg_two()))
//                        content += "<img src='" + note.getImg_two() + "'/>";
//                    if (!TextUtils.isEmpty(note.getImg_three()))
//                        content += "<img src='" + note.getImg_three() + "'/>";
//                    if (!TextUtils.isEmpty(note.getImg_four()))
//                        content += "<img src='" + note.getImg_four() + "'/>";
//
//                    RichText.fromHtml(content).into(mTxtNoteContent);
//                    mTxtNoteContent.setVisibility(View.VISIBLE);
//                }
//            }
        }
    }


    @Override
    public void onDestroyView() {

        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mResponseListener = null;
    }


    public QuestionEntry getQuestionEntry() {
        return mQuestionEntry;
    }

    private void notifyActivity() {
        selectedIds.clear();
        if (selectedMap != null && selectedMap.size() > 0) {
            for (String key : selectedMap.keySet()) {
                if (selectedMap.get(key)) {
                    selectedIds.add(key);
                }
            }

            mResponseListener.onMutil(selectedIds, position_whichOne);
        }
    }


}
